import request from '@/utils/request'
import { AxiosPromise } from 'axios'
import {
  OauthWechatResult,
  ImageCaptchaResult,
  EmailCaptchaResult,
  OauthVerifyData,
  OauthCheckData,
  OauthGithubResult,
  CodeParams,
  EmailData,
  OauthUserResult,
  OauthRegisterResult
} from './types'


export function imageCaptcha(): AxiosPromise<ImageCaptchaResult> {
  return request({
    url: '/api/oauth/captcha/image',
    method: 'get',
  })
}

/**
 * 获取邮箱验证码
 * */
export function emailCaptcha(data:string): AxiosPromise<EmailCaptchaResult> {
  return request({
    url: '/api/code/mail/' + data,
    method: 'get',
  })
}

/**
 * 用户注册
 * */
export function oauthRegister(data: OauthVerifyData): AxiosPromise<OauthRegisterResult> {
  return request({
    url: '/api/register/email',
    method: 'post',
    data
  })
}

/**
 * 用户登录
 * */
export function oauthLogin(data: any): AxiosPromise<OauthUserResult> {
  return request({
    url: '/api/auth/login',
    method: 'post',
    data
  })
}


/**
 * 重置密码
 * */
export function oauthResetPassword(data: any): AxiosPromise<OauthUserResult> {
  return request({
    url: '/api/auth/email/password',
    method: 'post',
    data
  })
}

export function oauthWechat(): AxiosPromise<OauthWechatResult> {
  return request({
    url: '/api/oauth/token/wechat',
    method: 'post',
  })
}

export function oauthTokenGithub(): AxiosPromise<OauthGithubResult> {
  return request({
    url: '/api/oauth/github/token',
    method: 'get',
  })
}

export function oauthCallbackGithub(params: CodeParams): AxiosPromise<OauthUserResult> {
  return request({
    url: '/api/oauth/github/callback',
    method: 'get',
    params,
  })
}
